package com.feng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.Exception.SystemException;
import com.feng.constants.SystemConstants;
import com.feng.domain.ResponseResult;
import com.feng.domain.dto.CategorySaveDto;
import com.feng.domain.entity.Article;
import com.feng.domain.entity.Category;
import com.feng.domain.enums.AppHttpCodeEnum;
import com.feng.domain.vo.CategoryPageVo;
import com.feng.domain.vo.CategoryVo;
import com.feng.domain.vo.PageVo;
import com.feng.mapper.CategoryMapper;
import com.feng.service.ArticleService;
import com.feng.service.CategoryService;
import com.feng.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2022-10-07 13:02:14
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ResponseResult getCategoryList() {
        //单表查询，查询已发布的文章表
        LambdaQueryWrapper<Article> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        //获取文章的分类id，并且去重
        List<Article> list = articleService.list(queryWrapper);
        //用java1.8 stream流新特性，创建流，中间过程用map处理流
        // 左边为单个list的对象，右边为实现方法，实现方法为提取分类id
        //由于要去重，所以最后将其转化为set集合自动去重
        Set<Long> collect = list.stream().map(e -> e.getCategoryId())
                .collect(Collectors.toSet());
        //得到分类id的集合，查询分类表
        List<Category> categories = listByIds(collect);
        //由于必须是正常的分类，现在查出的是所有分类，所以需要进行过滤操作,使用stream流filter进行过滤
        categories= categories.stream().
                filter(e -> e.getStatus().equals(SystemConstants.STATUS_NORMAL))
                .collect(Collectors.toList());
        //分装Vo
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public ResponseResult<CategoryVo> listAllCategoory() {
        //查询状态正常的
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getStatus, SystemConstants.STATUESS);
        List<Category> list = this.list(queryWrapper);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public ResponseResult pagecategorylist(Integer pageNum, Integer pageSize, String name, String status) {
        //分页查询
        Page<Category> page=new Page<>(pageNum,pageSize);
        //如果有传name，需要对name进行模糊查询
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name),Category::getName,name);

        //如果有传status，需要对status进行匹配查询
        queryWrapper.eq(StringUtils.hasText(status),Category::getStatus,status);

        this.page(page,queryWrapper);

        List<Category> records = page.getRecords();

        //拷贝对应的vo
        List<CategoryPageVo> categoryPageVos = BeanCopyUtils.copyBeanList(records, CategoryPageVo.class);

        PageVo pageVo=new PageVo(categoryPageVos,page.getTotal());
        //返回对应的值
        return ResponseResult.okResult(pageVo);

    }

    @Override
    public ResponseResult addCategory(CategorySaveDto categorySaveDto) {
        //判断新增的分类名称是否已存在
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getName,categorySaveDto.getName());
        //将Dto转化为对应的实体类
        List<Category> list = this.list(queryWrapper);
        if(list.size()>0){
            throw new SystemException(AppHttpCodeEnum.CATEGORY_IS_EXIST);
        }
        Category category = BeanCopyUtils.copyBean(categorySaveDto, Category.class);
        //新增用户
        this.save(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getCategoryById(Long id) {
        Category category = this.getById(id);
        CategoryPageVo categoryPageVo = BeanCopyUtils.copyBean(category, CategoryPageVo.class);
        return ResponseResult.okResult(categoryPageVo);
    }

    @Override
    public ResponseResult updateCategoryById(CategoryPageVo categoryPageVo) {
        Category category = BeanCopyUtils.copyBean(categoryPageVo, Category.class);

        updateById(category);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteCategoryById(List<Long> id) {
        for (Long aLong : id) {
            categoryMapper.deleteById(aLong);
        }

        return ResponseResult.okResult();
    }
}

